﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using System.IO;
using System.Windows.Forms;

namespace Terminal.Advanced_AEUC_settings_controls.Scripting
{
    /// <summary>
    /// Send message script action
    /// </summary>
    class ScrActPrepDCCFiles : ScrBasicAction
    {
        /// <summary>
        /// Public constructor for the script action
        /// </summary>
        /// <param name="iDispName">Action display text</param>
        /// <param name="iUsageHelp">Usage help and comments text for the action</param>
        /// <param name="callback">Callback function to be used for sending messages</param>
        public ScrActPrepDCCFiles(string iDispName, string iUsageHelp)
            : base(iDispName, iUsageHelp)
        {
            usageHelp += "Input params: DCC configuration file path + string containing comma separated sensor ids + \npayload structure reference + field of structure to store produced file path + field to store the produced file lenght\n";
        }

        /// <summary>
        /// Perform the action using specified parameters.
        /// </summary>
        /// <param name="paramList">List of the input/output parameters for the action</param>
        /// <returns>True if action is succesful</returns>
        public override bool Do(List<object> paramList)
        {
            if (breakPoint)
                WaitUserInput();

            if (paramList.Count > 4 && paramList[0].GetType() == typeof(char[]) && paramList[1].GetType() == typeof(char[])
                && paramList[2].GetType() == typeof(GenPayloadStruct) && paramList[3].GetType() == typeof(char[]) && paramList[4].GetType() == typeof(char[]))
            {
                string cfgDCC = new string(((char[])(paramList[0])));
                string sensors = new string(((char[])(paramList[1])));
                GenPayloadStruct pldStruct = (GenPayloadStruct)paramList[2];
                string fieldFName = new string(((char[])(paramList[3])));
                string fieldFSize = new string(((char[])(paramList[4])));

                FileInfo finf = new FileInfo(cfgDCC);

                if (!finf.Exists)
                {
                    finf = new FileInfo(Application.StartupPath + cfgDCC);
                }
                if (finf.Exists)
                {
                    List<string> senIds = new List<string>();

                    int indx = 0;
                    while (indx < sensors.Length)
                    {
                        string senid = Scripting.ScrCmdParser.ReadWord(ref indx, sensors);
                        if (senid == "")
                            break;
                        if (senid != ";")
                            senIds.Add(senid);
                    }
                    string tmpFile = Advanced_controls.UCTabPage.preparseDCCfile(finf, senIds.ToArray());

                    pldStruct.setStringItem(pldStruct[fieldFName], tmpFile);
                    pldStruct[pldStruct[fieldFSize]] = (new FileInfo(tmpFile)).Length;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Returns a List containing objects of the parameter list required for the Do function.
        /// </summary>
        /// <returns>List of parameter objects</returns>
        public override List<Type> GetParamList()
        {
            List<Type> paramList = new List<Type>();
            paramList.Add(typeof(char[]));
            paramList.Add(typeof(char[]));
            paramList.Add(typeof(GenPayloadStruct));
            paramList.Add(typeof(char[]));
            paramList.Add(typeof(char[]));

            return paramList;
        }
    }
}




